package com.sdk.springbootadvancedsearch.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdk.springbootadvancedsearch.domain.request.QueryRequest;
import com.sdk.springbootadvancedsearch.domain.response.PageResult;
import com.sdk.springbootadvancedsearch.entity.SchemaSampleModelsEntity;
import com.sdk.springbootadvancedsearch.service.QueryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
public class QueryController {
    private final QueryService queryService;

    public QueryController(QueryService queryService) {
        this.queryService = queryService;
    }

    @PostMapping("/samples")
    public ResponseEntity<PageResult<SchemaSampleModelsEntity>> querySamplePage(
          @RequestBody @Valid QueryRequest request) {
        Page<SchemaSampleModelsEntity> page = queryService.executeQuery(request);

        PageResult<SchemaSampleModelsEntity> result = new PageResult<>();
        result.setData(page.getRecords());
        result.setTotal(page.getTotal());
        result.setPage(request.getPage());
        result.setPageSize(request.getPageSize());

        return ResponseEntity.ok(result);
    }

    @PostMapping("/samples/dropdowns-list")
    public ResponseEntity<List<SchemaSampleModelsEntity>> queryProjects(
            @RequestBody QueryRequest request) {
        List<SchemaSampleModelsEntity> list = queryService.executeQueryWithoutPage(request);
        return ResponseEntity.ok(list);
    }
}